package cn.ml.sheJiMoShi.single;

//new的时候调用构造方法(不管是不是public还是private)
public class SingletonLazy {
    public static void main(String[] args) {
        President p1 = President.getPresident();
        p1.getName();
        President p2 = President.getPresident();
        p2.getName();
        if(p1 == p2){
            System.out.println("他们是同一个人");
        }else {
            System.out.println("他们不是同一个人");
        }
    }


}

class  President {

    private static President president = null;

    private President(){
        System.out.println("产生一个总统！");
    }

    public static President getPresident(){
        //president = new President();
        if (president == null){
            president = new President();
        }else {
            System.out.println("已经有一个总统了，不能再生产新的总统了！");
        }
        return president;
    }

    public void getName(){
        System.out.println("我是美国总统：特朗普！");
    }
}


